import React from 'react';
import { connect } from 'dva';
import { Row,Col,Table,Popconfirm } from 'antd';
import DefinitionList from '../../../components/DefinitionList';  
import TablePagination from './TablePagination';
import EditBenchmark from './EditBenchmark';
import style from './benchmarkTable.less';
/**
 * [BenchmarkListTable 基准表格]
 */
let columns;
class BenchmarkListTable extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      data:this.props.benchmarkLists,
      page:this.props.benchmarkPage,
      BenchmarkList:this.props.benchmarksList
    }
    columns = [
      { title: '基准Id', dataIndex: 'benchId', key: 'benchId' },
      { title: '基准代码', dataIndex: 'benchCode', key: 'benchCode' },
      { title: '基准名称', dataIndex: 'benchName', key: 'benchName' },
      { title: '绝对收益', dataIndex: 'baseRate', key: 'baseRate' },
      { title: '创建日期', dataIndex: 'establish', key: 'establish' },
      { title: '创建人', dataIndex: 'creator', key: 'creator' },
      { 
        title: '个人&公用标签', 
        dataIndex: 'isShare', 
        key: 'isShare',
        render:(text,index)=>{
          if(text=='0'){
            return(
              <span>个人标签</span>
            )
          }else{
            return(
              <span>公用标签</span>
            )
          }
        }
      },
      { title: '描述', dataIndex: 'description', key: 'description' },
      {
        title: '操作',
        key: 'action',
        render: item => {
            if(item.benchCode !== null || item.benchId == 0){
              return null;
            }else{
              return(
                <Row type="flex" justify="center">
                  <EditBenchmark data={item} BenchmarkList={this.state.BenchmarkList}/>&nbsp;&nbsp;
                  <Popconfirm title="确定删除?"  cancelText="取消" okText="确定" onConfirm={() => this.deleteBench(item.benchId)}>
                    <a>删除</a>
                  </Popconfirm>
                </Row>
              )
            }
          }
        }
    ];
  }
  componentDidMount() {
    this.fetchData(this.props)
  }
  componentWillReceiveProps(nextProps) {
    console.log(1122,this.props,2233,nextProps)
    if(nextProps.benchmarkLists!==this.props.benchmarkLists){
      this.setState({
        data:nextProps.benchmarkLists,
        page:nextProps.benchmarkPage,
        BenchmarkList:nextProps.benchmarksList
      })
    }
    if(nextProps.benchmarksList!==this.props.benchmarksList){
      this.setState({
        BenchmarkList:nextProps.benchmarksList
      })
    }
  }
  fetchData=(props)=>{
    this.props.dispatch({
      type: `benchmarkMgt/getBenchmark`,
      payload:props
    })
    this.props.dispatch({
      type: `benchmarkMgt/getBenchmarks`,
      payload:props
    })
  }
  deleteBench=(v)=>{
    this.props.dispatch({
      type: `benchmarkMgt/deleteBenchmark`,
      payload:{v}
    })
  }
  render() {
    const { data,page } = this.state
    console.log(55667777,this.state)

    return (
      <div>
        <Table
          columns={columns}
          rowKey={record =>record.benchId}
          pagination={false}
          expandedRowRender={
            record => (
              record.weight.map((item,i)=>{
                return(
                  <Row key={i} type="flex" justify="start">
                    <Col>
                      <DefinitionList name='基准公开代码:  ' value={item.benchmarkCode} className={style.ffff}/>
                    </Col>
                    <Col>
                      <DefinitionList name='基准公开名称:  ' value={item.benchmarkName} className={style.ffff}/>
                    </Col>
                    <Col>
                      <DefinitionList name='基准占比:  ' value={item.benchmarkRate} className={style.ffff}/>
                    </Col>
                  </Row>
                )
              })
              
            )
          }
          dataSource={data}
        />
        <TablePagination
          page={page}
          tag='benchmarkMgt'
          dispatch={this.props.dispatch}
        />
      </div>
    )
  }
}

BenchmarkListTable.defaultProps={
  benchmarkList:[]
}

export default connect()(BenchmarkListTable)
